package com.ckk.shiro.realm;


import com.ckk.shiro.entity.User;
import com.ckk.shiro.service.PermissionService;
import com.ckk.shiro.service.RoleService;
import com.ckk.shiro.service.UserService;
import com.ckk.shiro.vo.LoginUserVo;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/**
 * @author 陈昆昆
 * @version 1.0
 * @date 2020/4/9 9:48
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserService userServiceImpl;
    
    @Autowired
    private RoleService roleServiceImpl;
    
    @Autowired
    private PermissionService permissionServiceImpl;
    

    @Override
    public String getName() {
        return this.getClass().getSimpleName();
    }

    /**
     * 身份验证(为当前登录用户验证身份)
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取当前对象用户名
        String userName = (String) token.getPrincipal();
        try {
            //根据用户名获取对象
            User user = userServiceImpl.findUserByUserName(userName);
            //判断对象是否为空
            if (user != null) {
                //查询角色信息
                Set<String> roles = roleServiceImpl.findRoleListByUserid(user.getUserid());
                //查询权限
                Set<String> permisson = permissionServiceImpl.findRoleListByUserId(user.getUserid());
                //创建用户登录对象 用户信息 角色信息 权限信息
                LoginUserVo loginUserVo = new LoginUserVo(user, roles, permisson);
                //获取盐值(用户名作为颜值)
                ByteSource salt = ByteSource.Util.bytes(user.getUsername());
                //创建身份验证对象
                SimpleAuthenticationInfo info = new
                        SimpleAuthenticationInfo(loginUserVo,user.getUserpwd(),
                        salt,this.getName());
                return info;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 授权(为当前登录的用户授予相应的权限)
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        LoginUserVo loginUserVo = (LoginUserVo) principalCollection.getPrimaryPrincipal();
        Set<String> roles = loginUserVo.getRoles();
        Set<String> permissions = loginUserVo.getPermissions();
        //创建授权对象
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        if (roles!=null&&roles.size()>0){
            info.setRoles(roles);
        }
        if (permissions!=null&&permissions.size()>0){
            info.setStringPermissions(permissions);
        }
        return info;
    }


}
